package com.woniuxy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniuxy.controller.form.*;
import com.woniuxy.controller.info.UserAndAccountInfo;
import com.woniuxy.controller.info.UserAndRoleInfo;
import com.woniuxy.controller.info.UserAndRolesInfo;
import com.woniuxy.controller.info.UserInfo;
import com.woniuxy.model.ProjectRoleUser;
import com.woniuxy.model.User;
import com.woniuxy.service.ProjectRoleUserService;
import com.woniuxy.service.UserService;
import com.woniuxy.utils.MyException;
import com.woniuxy.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 马世亮
 * @since 2022-02-15
 */
@Api(tags = "用户接口")
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;
    @Resource
    private ProjectRoleUserService projectRoleUserService;

    /**
     * 获取登录的用户信息
     * @return
     */
    @ApiOperation("获取登录的用户信息")
    @PostMapping("/getmessage")
    public Result getUserMessage(){
        UserInfo userInfo = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        QueryWrapper<User> wrapper = new QueryWrapper<User>();
        wrapper.eq("account_id",userInfo.getId());
        User user = userService.getOne(wrapper);
        if(user==null){

            return Result.fail(400,"用户未绑定");
        }
        return Result.success(200,"查询成功",user);
    }

    /**
     * 查询某项目的所有成员
     * @param projectIdForm
     * @return
     * @throws MyException
     */
    @PostMapping("/getallusers")
    @ApiOperation("查询某项目的所有成员")
    public Result getallusers(@RequestBody ProjectIdForm projectIdForm) throws MyException {
        System.out.println(projectIdForm);//ProjectIdInfo(pid=3)

        List<UserAndRoleInfo> list = userService.getUsers(projectIdForm);

        if(null == list) throw new MyException(500,"查询失败");
        return Result.success(200,"查询成功",list);
    }

    /**
     * 操作成员的角色
     * @param userNewRoleForm 新增成员的项目角色信息
     * @return
     * @throws MyException
     */
    @PostMapping("/giveuserrole")
    @ApiOperation("操作成员的角色")
    public Result giveUserRole(@RequestBody UserNewRoleForm userNewRoleForm) throws MyException {
        System.out.println(userNewRoleForm);
        boolean b = userService.giveUserRole(userNewRoleForm);
        if(!b) return Result.fail(500,"成员授权失败");
        return Result.success(200,"成员授权成功",null);
    }

    /**
     * 查询某项目的成员
     * @param projectIdForm 项目信息
     * @return  用户及项目角色信息
     * @throws MyException
     */
    @ApiOperation("查询某项目的成员")
    @PostMapping("/getuserBypid")
    public Result getuserBypid(@RequestBody ProjectIdForm projectIdForm) throws MyException {
        System.out.println(projectIdForm);

        IPage<UserAndRolesInfo> list = userService.getUserAndRole(projectIdForm);
        System.out.println(list);
        if(null == list) throw new MyException(500,"查询失败");
        return Result.success(200,"查询成功",list);
    }

    /**
     * 移除成员的角色
     * @param userRoleForm 用户及项目角色信息
     * @return
     */
    @ApiOperation("移除成员的角色")
    @PostMapping("/removeuserrole")
    public Result removeUserrole(@RequestBody UserRoleForm userRoleForm){
        System.out.println(userRoleForm);

        boolean b = userService.removerole(userRoleForm);
        if(!b) return Result.fail(500,"移除失败");
        return Result.success(200,"移除成功",null);
    }

    /**
     * 查询所有员工信息
     * @param pageForm 分页信息
     * @return  用户及账号信息
     * @throws MyException
     */
    @ApiOperation("查询所有员工信息")
    @PostMapping("/getAlluser")
    public Result getAllUser(@RequestBody PageForm pageForm) throws MyException {
        System.out.println(pageForm);

        IPage<UserAndAccountInfo> list = userService.getAllusers(pageForm);
        if(null == list) throw new MyException(500,"获取员工信息失败");
        return Result.success(200,"成功获取员工信息",list);
    }

    /**
     *  根据用户名或者id查询
     * @param userinfoForm  用户名或者id
     * @return 用户信息
     */
    @ApiOperation("模糊查询用户")
    @PostMapping("/getuserByinfo")
    public Result getUserByinfo(@RequestBody UserinfoForm userinfoForm){
        System.out.println(userinfoForm);

        List<User> u = userService.getuserByinfo(userinfoForm.getUserinfo());
        if(null == u) return Result.fail(500,"查询失败");
        return Result.success(200,"查询成功",u);
    }

    /**
     * 添加新用户
     * @param newuserForm 新用户的信息
     * @return
     * @throws MyException
     */
    @ApiOperation("添加新用户")
    @PostMapping("/savenewuser")
    public Result saveNewuser(@RequestBody NewuserForm newuserForm) throws MyException {
        System.out.println(newuserForm);

        boolean b = userService.addnewuser(newuserForm);
        if(!b) return Result.fail(500,"新增失败，请稍后重试");
        return Result.success(200,"添加成功",null);
    }

    /**
     * 修改用户信息
     * @param userChangeForm 用户及账号信息
     * @return
     * @throws MyException
     */
    @ApiOperation("修改用户信息")
    @PostMapping("/savechangeuser")
    public Result savechangeuser(@RequestBody UserChangeForm userChangeForm) throws MyException {
        System.out.println(userChangeForm);

        boolean b = userService.changeuser(userChangeForm);
        if(!b) return Result.fail(500,"修改失败");
        return Result.success(200,"修改成功",null);
    }

    /**
     *  删除某用户
     * @param userAndAccountInfo 用户及账号信息
     * @return
     * @throws MyException
     */
    @ApiOperation("删除某用户")
    @PostMapping("/removeuser")
    public Result removeUser(@RequestBody UserAndAccountInfo userAndAccountInfo) throws MyException {
        System.out.println(userAndAccountInfo);

        boolean b = userService.removeuser(userAndAccountInfo);
        if(!b) return Result.fail(500,"删除失败");

        return Result.success(200,"删除成功",null);
    }

    /**
     * 批量删除某些用户
     * @param userManyForm 用户及账号信息
     * @return
     * @throws MyException
     */
    @ApiOperation("删除某些用户")
    @PostMapping("/removemany")
    public Result removeManyuser(@RequestBody UserManyForm userManyForm) throws MyException {
        System.out.println(userManyForm);

        boolean b = userService.removemany(userManyForm);
        if(!b) return Result.fail(500,"删除失败，请稍后重试");
        return Result.success(200,"删除成功",null);
    }

    /**
     * 修改用户的项目角色
     * @param userNewRoleForm 项目角色及成员信息
     * @return
     * @throws MyException
     */
    @ApiOperation("修改用户的项目角色")
    @PostMapping("/changeuserrole")
    public Result changeUserRole(@RequestBody UserNewRoleForm userNewRoleForm) throws MyException {
        System.out.println(userNewRoleForm);

        boolean b = userService.changeuserrole(userNewRoleForm);

        if(!b) return Result.fail(500,"修改失败");
        return Result.success(200,"修改成功",null);
    }

    /**
     * 获取已有角色的用户
     * @param projectIdForm 项目的编号信息
     * @return 用户及项目角色信息
     */
    @ApiOperation("获取已有角色的用户")
    @PostMapping("/getHasroleUser")
    public Result getHasroleUser(@RequestBody ProjectIdForm projectIdForm){
        System.out.println(projectIdForm);

        List<UserAndRolesInfo> list = userService.getHasroleUser(projectIdForm.getPid());
        if(null == list) return Result.fail(500,"未获取到已有角色的用户");
        return Result.success(200,"获取成功",list);
    }


}


